package com.dty.common.utils;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.WeekFields;
import java.util.Locale;

/**
 * @ClassName DateUtils
 * @Description 日期工具类 jdk8
 * @Author 杨钧生
 * @Date 2020/12/24 13:39
 */
public class DateUtils {


    /**
     * 周的第一天或最后一天
     *
     * @param today   时间
     * @param isFirst true 表示开始时间，false表示结束时间
     * @return LocalDate 日期
     */
    public static LocalDate getStartOrEndDayOfWeek(LocalDate today, Boolean isFirst) {
        LocalDate resDate = LocalDate.now();
        if (today == null) {
            today = resDate;
        }
        DayOfWeek week = today.getDayOfWeek();
        int value = week.getValue();
        if (isFirst) {
            resDate = today.minusDays(value - 1);
        } else {
            resDate = today.plusDays(7 - value);
        }
        return resDate;
    }

    /**
     * 时间属于年的第几周
     *
     * @param today 时间
     * @return int 周数
     */
    public static int getWeekNumber(LocalDate today) {
        LocalDate resDate = LocalDate.now();
        if (today == null) {
            today = resDate;
        }
        WeekFields weekFields = WeekFields.of(Locale.getDefault());
        int weekNumber = today.get(weekFields.weekOfWeekBasedYear());
        return weekNumber;
    }

    /**
     * 月的第一天或最后一天
     *
     * @param today   时间
     * @param isFirst true 表示开始时间，false表示结束时间
     * @return LocalDate 日期
     */
    public static LocalDate getStartOrEndDayOfMonth(LocalDate today, Boolean isFirst) {
        LocalDate resDate = LocalDate.now();
        if (today == null) {
            today = resDate;
        }
        Month month = today.getMonth();
        int length = month.length(today.isLeapYear());
        if (isFirst) {
            resDate = LocalDate.of(today.getYear(), month, 1);
        } else {
            resDate = LocalDate.of(today.getYear(), month, length);
        }
        return resDate;
    }

    /**
     * 季度的第一天或最后一天
     *
     * @param today   时间
     * @param isFirst true 表示开始时间，false表示结束时间
     * @return LocalDate 日期
     */
    public static LocalDate getStartOrEndDayOfQuarter(LocalDate today, Boolean isFirst) {
        LocalDate resDate = LocalDate.now();
        if (today == null) {
            today = resDate;
        }
        Month month = today.getMonth();
        Month firstMonthOfQuarter = month.firstMonthOfQuarter();
        Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
        if (isFirst) {
            resDate = LocalDate.of(today.getYear(), firstMonthOfQuarter, 1);
        } else {
            resDate = LocalDate.of(today.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear()));
        }
        return resDate;
    }

    /**
     * 时间属于第几季度
     *
     * @param today 时间
     * @return int 季度
     */
    public static int getQuarterNumber(LocalDate today) {
        LocalDate resDate = LocalDate.now();
        if (today == null) {
            today = resDate;
        }
        int month = today.getMonth().getValue();

        if (month == 1 || month == 2 || month == 3) {
            return 1;
        } else if (month == 4 || month == 5 || month == 6) {
            return 2;
        } else if (month == 7 || month == 8 || month == 9) {
            return 3;
        } else {
            return 4;
        }

    }

    /**
     * 季年的第一天或最后一天
     *
     * @param today   时间
     * @param isFirst true 表示开始时间，false表示结束时间
     * @return LocalDate 日期
     */
    public static LocalDate getStartOrEndDayOfYear(LocalDate today, Boolean isFirst) {
        LocalDate resDate = LocalDate.now();
        if (today == null) {
            today = resDate;
        }
        if (isFirst) {
            resDate = LocalDate.of(today.getYear(), Month.JANUARY, 1);
        } else {
            resDate = LocalDate.of(today.getYear(), Month.DECEMBER, Month.DECEMBER.length(today.isLeapYear()));
        }
        return resDate;
    }

}
